var c = require('../lib/common.js');
var cf = require('../lib/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    status: 1,
    action: 1,
    beizhu:'暂无'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    c.verlogin(this);
    var action = typeof (options) == 'object' ? 1 : options;
    wx.showLoading({
      title: '加载中...',
    })
    var uid = wx.getStorageSync('userinfo').id;
    var page = this.data.page;
    var status = this.data.status;
    this.setData({
      page: page + 1
    });
    c.ajax(
      cf.hostapi + 'getOrderList', {
        uid: uid, page: page, status: status
      },
      res => {
        console.log(res);
        if(res.data.retcode==200){
          wx.hideLoading();
          var list = res.data.data;
          console.log(list);
          this.setData({ list: list, action: action })
        };
      }
    );
  },
  userinfo: function (e) {
    c.clicklogin(e, this);
  },
  orderclick: function (e) {
    var action=e.currentTarget.dataset.action;
    this.setData({
      page: 0, status: action
    })
    this.onLoad(action);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that=this;
    wx.showLoading({
      title: '加载中...',
    })
    var uid = wx.getStorageSync('userinfo').id;
    var action = that.data.action;
    var page = that.data.page;
    that.setData({ page: page + 1})
    c.ajax(
      cf.hostapi + 'getOrderList', {
        uid: uid, page: page, status: action
      },
      res => {
        if (res.data.retcode == 200) {
          wx.hideLoading();
          var list  = that.data.list;
          var listn = list.concat(res.data.data);
          console.log(page);
          that.setData({ list: listn, action: action})
        };
      }
    );
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  makecall: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  revokeOrder(e){
    var that=this;
    wx.showLoading({
      title: '退单中...',
    })
    var id=e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    c.ajax(
      cf.hostapi +'revokeOrder',
      { order_id: id},
      res=>{
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '退单成功',
            image:'/images/dui.png'
          })
          var list = that.data.list;
          list.splice(index,1);
          console.log(list);
          that.setData({ list: list})
        };
      }
    )
  }
})